<template>
  <div>

    <div class="breadcrumb-wrap">
      <el-breadcrumb>
        <el-breadcrumb-item>数据列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="opration-wrap">
      <el-button type="primary">
        <router-link :to="{path: '/list/add'}">新增数据</router-link>
      </el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="D_Name" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="D_Code" label="code" width="180">
      </el-table-column>
      <el-table-column prop="D_ObjectID" label="ObjectID">
      </el-table-column>
      <el-table-column prop="D_ParentID" label="ParentID">
      </el-table-column>
      <el-table-column prop="D_Sort" label="sort" width="150px">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small">
            <router-link :to="{path: '/list/add',query:{objid: scope.row.D_ObjectID,pageindex: page.pageindex}}">编辑</router-link>
          </el-button>
          <el-button type="text" size="small" @click="toWarn(scope.row.D_ObjectID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-wrap" v-if="page.total > page.pageindex">
      <el-pagination background layout="prev, pager, next" :total="page.total" :current-page="parseInt(page.pageindex)" @current-change="handleCurrentChange">
      </el-pagination>
    </div>

  </div>
</template>

  <script>
import Config from "@/config/config.js"
export default {
  data() {
    return {
      type: "getdictionarymodellistwithpage",
      tableData: [],
      page: {
        total: 0,
        pageindex: this.$route.query.pageindex || 1,
        pagesize: 10
      }
    };
  },

  methods: {
    async getList() {
      let _this = this;

      let r = await _this.$http.get(Config.sync, {
        params: {
          type: _this.type,
          pageindex: _this.page.pageindex,
          pagesize: _this.page.pagesize
        }
      });

      if (!r || !r.body.Result) {
        _this.$message.error("获取数据失败，请稍后重试");
        return false;
      }

      let data = r.body;

      [..._this.tableData] = data.Data.data;
      _this.page.total = data.Data.total;
    },
    handleCurrentChange(val) {
      this.page.pageindex = `${val}`;
      this.$router.push({ query: { pageindex: val } });
      this.getList();
    },
    toWarn(objid) {
      let _this = this;

      _this
        .$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
          _this.deleteObj(objid);
        })
        .catch(() => {});
    },
    async deleteObj(objid) {
      let _this = this;

      let r = await _this.$http.get(Config.sync, {
        params: {
          type: "getordeldicmodel",
          flagtype: 2,
          objid: objid
        }
      });

      if (!r || !r.body.Result) {
        _this.$message.error("删除失败，请稍后重试");
        return false;
      }

      _this.$message({
        type: "success",
        message: "删除成功!"
      });

      _this.getList();
    }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style>
.opration-wrap {
  padding: 10px 0;
}
.opration-wrap a {
  color: #fff;
}
.pagination-wrap {
  padding: 20px 0;
}
</style>
